﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using BusinessLayer.RegInfo;
using BusinessLayer.RegInfo.Entities;
using BackEnd.Common;
using BackEnd.Models;

namespace BackEnd.Controllers
{
    public class SubscriptionController : Controller
    {
        //
        // GET: /Subscription/
        public ActionResult Index()
        {
            var subBL = new SubscriptionBL();
            var lstSubscriptions = new List<Subscription>();
            lstSubscriptions.AddRange(subBL.GetAll());

            return View("Index", lstSubscriptions);
        }

        //
        // GET:/Subscription/Create
        [CustomAuthorize(Action = Constants.AdminActions.CreateSubscription)]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.CreateSubscription)]
        public ActionResult Create(SubscriptionModel model)
        {
            if (ModelState.IsValid)
            {
                var subsBL = new SubscriptionBL();
                var check = subsBL.GetByCode(model.Code);
                if (check == null)
                {
                    var subs = new Subscription();
                    subs.Code = model.Code;
                    subs.Title = model.Title;
                    subs.Status = Common.Constants.Status.ACTIVE;
                    subs.NumberOfDays = model.NumberOfDays;
                    subs.Price = model.Price;

                    int result = subsBL.CreateSubscription(subs);
                    if (result > 0)
                    {                        
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        ViewData["ErrorMessage"] = "Has an error when executing insert subscription into database"; 
                    }
                }
                else
                {
                    ViewData["ErrorMessage"] = String.Format("This subscription - {0} exists!",model.Code); 

                }

            }

            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [CustomAuthorize(Action = Constants.AdminActions.DeleteSubscription)]
        public ActionResult Delete(string id)
        {
            
                var subsBL = new SubscriptionBL();
                if (!String.IsNullOrEmpty(id))
                {
                    List<string> lstOfSubsIDs = new List<string>();
                    foreach (string item in id.Split('_'))
                    {
                        int temp = -1;
                        Int32.TryParse(item, out temp);
                        if (temp > 0)
                        {
                            lstOfSubsIDs.Add(item);
                        }
                    }
                    int result = subsBL.DeleteSubscriptionByIds(lstOfSubsIDs);
                    if (result > 0)
                    {                       
                        //return Json(1, JsonRequestBehavior.AllowGet);
                        ViewData["ErrorMessage"] = "Delete successful!";
                        return RedirectToAction("Index");
                    }
                    
                }
                //return Json(0, JsonRequestBehavior.AllowGet);
                ViewData["ErrorMessage"] = "Cannot delete selected items!";
                return RedirectToAction("Index");

            
        }

        //
        // GET:/Subscription/Edit/id
        //[AcceptVerbs(HttpVerbs.Get)]
        [CustomAuthorize(Action = Constants.AdminActions.UpdateSubscription)]
        public ActionResult Edit(int id)
        {
            var subBL = new SubscriptionBL();
            var subs = subBL.GetBySubscriptionID(id);
            var subModel = new SubscriptionModel();
            subModel.SubscriptionID = subs.SubscriptionID;
            subModel.Code = subs.Code;
            subModel.Title = subs.Title;
            subModel.NumberOfDays = subs.NumberOfDays;
            subModel.Price = subs.Price;
            subModel.Status = subs.Status;

            var lstStatus = Common.Constants.Status.GetListStatus();
            ViewBag.ListStatus = lstStatus.ToArray();



            return View("Edit", subModel);
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.UpdateSubscription)]
        public ActionResult Edit(SubscriptionModel model)
        {
            if (ModelState.IsValid)
            {
                var subsBL = new SubscriptionBL();
                var check = subsBL.GetBySubscriptionID(model.SubscriptionID);
                if (check != null)
                {
                    var subs = new Subscription();
                    subs.SubscriptionID = model.SubscriptionID;
                    subs.Code = model.Code;
                    subs.Title = model.Title;
                    subs.Status = model.Status;
                    subs.NumberOfDays = model.NumberOfDays;
                    subs.Price = model.Price;

                    int result = subsBL.UpdateSubscription(subs);
                    if (result > 0)
                    {                        
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        ViewData["ErrorMessage"] = "Has an error when executing insert subscription into database";
                    }
                }
                else
                {
                    ViewData["ErrorMessage"] = String.Format("This subscription - {0} not exists!", model.Code);

                }

            }
            var lstStatus = Common.Constants.Status.GetListStatus();
            ViewData["ListStatus"] = lstStatus.ToArray();
            return View(model);
        }


    }
}
